<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>民情日志</el-breadcrumb-item>
      <el-breadcrumb-item>全部日志</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="5">
          <el-input placeholder="请输入单号" v-model="queryInfo.orderId" clearable @clear="getLogs">
            <el-button slot="append" icon="el-icon-search" @click="getLogs"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" style="padding-left:20px">
          <el-date-picker v-model="queryInfo.queryTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日" @change="getLogs">
          </el-date-picker>
        </el-col>
        <el-col :span="4" style="padding-left:20px">
          <el-button type="primary" @click="ShowAddLogsdialogVisible" >添加日志</el-button>
        </el-col>
      </el-row>

      <el-table :data="LogsList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="日志单号" prop="logsId"></el-table-column>
        <el-table-column label="日志详情" prop="detail"></el-table-column>
        <el-table-column label="工作人员ID">
          <template slot-scope="{row}">
            <el-link @click="showWorkerInfoDialog(row.workId)">{{row.workId}}<i class="el-icon-view el-icon--right"></i> </el-link>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{row}">
            <span>{{row.createTime | dataFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间">
          <template slot-scope="{row}">
            <span>{{row.updateTime | dataFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="233px">
          <template slot-scope="{row}">
            <el-tooltip class="item" effect="dark" content=" 修改" placement="top">
              <el-button type="primary" icon="el-icon-circle-plus" size="mini" @click="ShowEditLog(row.logsId)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button type="warning" icon="el-icon-delete" size="mini" @click="deleteLog(row.logsId)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 30, 50]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align:center" background>
      </el-pagination>
    </el-card>

    <el-dialog title="工作人员信息" :visible.sync="WorkerdialogVisible" width="50%">
      <el-form :model="Info" label-width="100px" disabled>
        <el-form-item label="工号:">
          <el-input v-model="Info.workId"></el-input>
        </el-form-item>
        <el-form-item label="姓名:">
          <el-input v-model="Info.workName"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="Info.workPhone"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="Info.workPhone"></el-input>
        </el-form-item>
        <el-form-item label="工作社区:">
          <el-input v-model="Info.community"></el-input>
        </el-form-item>
        <el-form-item label="工作详细地址:">
          <el-input v-model="Info.address"></el-input>
        </el-form-item>
        <el-form-item label="职位:">
          <el-input v-model="Info.jobs"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="修改日志信息" :visible.sync="editdialogVisible" width="50%">
      <el-form :model="editForm" label-width="100px" >
        <el-form-item label="日志记录" prop="detail">
          <el-input type="textarea" v-model="editForm.detail" :rows="6"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editLogs">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加日志信息" :visible.sync="addLogsdialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="addForm" label-width="100px"  ref="addFormRef" :rules="rules">
        <el-form-item label="日志记录" prop="detail">
          <el-input type="textarea" v-model="addForm.detail" :rows="6" placeholder="请输入不超过255字符"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addLogs">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/js/cityData.js'
export default {
  data () {
    return {
      queryInfo: {
        LogsId: '',
        pageNum: 1,
        pageSize: 5,
        queryTime: '',
        workId: window.sessionStorage.getItem('userId')
      },
      LogsList: [],
      total: 0,
      cityData,
      WorkerdialogVisible: false,
      editdialogVisible: false,
      Info: {},
      editForm: {
        detail: ''
      },
      addLogsdialogVisible: false,
      addForm: {
        detail: ''
      },
      rules: {
        detail: [{ min: 5, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur' },
          { required: true, message: '请输入信息', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.getLogs()
  },
  methods: {
    async getLogs () {
      if (this.queryInfo.queryTime === null) this.queryInfo.queryTime = ''
      const { data: res } = await this.$http.get('logs/getLogs', { params: this.queryInfo })
      this.LogsList = res.data.records
      this.total = res.data.total
    },
    // 分页器
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getLogs()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pageNum = newSize
      this.getLogs()
    },
    async showWorkerInfoDialog (id) {
      const { data: res } = await this.$http.get(`work/getWorkerById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取信息失败')
      }
      this.Info = res.data
      this.WorkerdialogVisible = true
    },
    async ShowEditLog (id) {
      const { data: res } = await this.$http.get(`logs/getLogsById/${id}`)
      this.editForm = res.data
      this.editdialogVisible = true
    },
    resetEditForm () {
      this.$refs.editFormRef.resetFields()
    },
    async editLogs () {
      const { data: res } = await this.$http.put(`logs/editLogsById/${this.editForm.logsId}`, this.editForm)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      this.$message.success('修改成功')
      this.editdialogVisible = false
      this.getLogs()
    },
    async deleteLog (id) {
      const confirm = await this.$confirm('此操作将永久删除该日志信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        console.log(err)
      })
      if (confirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`logs/deleteLogById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getLogs()
    },
    ShowAddLogsdialogVisible () {
      this.addLogsdialogVisible = true
    },
    resetAddForm () {
      this.addLogsdialogVisible = false
      this.$refs.addFormRef.resetFields()
    },
    async addLogs () {
      const { data: res } = await this.$http.post('logs/addLogs', {
        workId: this.queryInfo.workId,
        detail: this.addForm.detail
      })
      console.log(res)
      if (res.meta.status !== 200) {
        this.addWorkerdialogVisible = false
        return this.$message.error('添加失败')
      }
      // console.log(res)
      this.$message.success('成功')
      this.addLogsdialogVisible = false
      this.getLogs()
    }
  }
}
</script>

<style lang='less' scoped>
</style>
